package edu.sctu.yueding.wxapi;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import edu.sctu.yueding.R;
import edu.sctu.yueding.activitys.BaseActivity;
import edu.sctu.yueding.utils.Constants;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.view.CustomDialog;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    private TextView wxBack;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
        initSystemBar(this, 0);
        wxBack = (TextView) findViewById(R.id.wx_back);

        wxBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            final CustomDialog.Builder builder = new CustomDialog.Builder(this);

            if (resp.errCode == 0) {

                builder.setMessage("提示：支付成功，可通过个人中心查看！");
                builder.setPositiveButton("立即查看", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss(); //关闭dialog

                        Bundle bundle = new Bundle();
                        bundle.putInt("flag", 3);
                        JumpUtils.activityToFragmentJump(WXPayEntryActivity.this, bundle);
                        finish();
                    }
                });

                builder.create().show();

            } else if (resp.errCode == -1) {

                AlertDialog.Builder builderError = new AlertDialog.Builder(this);
                builderError.setTitle(R.string.app_tip);
                builderError.setMessage("提示：支付遇到问题,请稍候再试");
                builderError.show();


            } else if (resp.errCode == -2) {

                AlertDialog.Builder builderCancel = new AlertDialog.Builder(this);
                builderCancel.setTitle(R.string.app_tip);
                builder.setMessage("提示：取消支付！");
                builderCancel.show();

            }


        }
    }

}